﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Churchtoolkit.Web.Shell.Presenters;
using Churchtoolkit.Web.Shell.Views;
using System.IO;
using Churchtoolkit.Utils.Navigation;
using System.Configuration;

namespace Churchtoolkit.Web
{
    public partial class UserProfileView : Churchtoolkit.Utils.MVP.MasterPage<UserProfilePresenter> , IUserProfileView
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillMemberInfo();
                SetSelectedLink();
            }
        }

        private void SetSelectedLink()
        {
            switch (Path.GetFileNameWithoutExtension(this.Request.PhysicalPath.ToLower()))
            {
                case "personalinfoview":
                    this.liPersonalInfo.Attributes.Add("class", "ui-state-default ui-corner-top ui-tabs-selected ui-state-active");
                    break;
                case "churchinformationview":
                    this.liChurchInfo.Attributes.Add("class", "ui-state-default ui-corner-top ui-tabs-selected ui-state-active");
                    break;
                case "educationworkview":
                    this.liEducation.Attributes.Add("class", "ui-state-default ui-corner-top ui-tabs-selected ui-state-active");
                    break;
                case "modulesview":
                    this.liModules.Attributes.Add("class", "ui-state-default ui-corner-top ui-tabs-selected ui-state-active");
                    break;
            }
        }

        private void FillMemberInfo()
        {
            var member = this.Presenter.GetMember();
            this.lblEmail.Text = member.Email;
            this.lblFullName.Text = string.Format("{0} {1}", member.FirstName, member.LastName);

            if (!string.IsNullOrEmpty(member.ImagePath))
            {
                string memberProfilePicPath = ConfigurationManager.AppSettings["UploadsFolder"] + member.ImagePath;
                string memberProfilePicPhysicalPath = Server.MapPath(memberProfilePicPath);
                if (File.Exists(memberProfilePicPhysicalPath))
                    this.imgMemberImage.ImageUrl = memberProfilePicPath;
            }
        }

        protected void btnChangeProfileImage_Click(object sender, EventArgs e)
        {
            Navigator.NavigateTo(Pages.Account_ChangeProfilePicture);
        }

        protected void btnChangePassword_Click(object sender, EventArgs e)
        {
            Navigator.NavigateTo(Pages.Account_ChangePassword);
        }
    }
}